<?php
// +----------------------------------------------------------------------
// | author: thinkphp
// +----------------------------------------------------------------------

namespace app\adminapi\validate\user;


use app\common\model\user\User;
use app\common\validate\BaseValidate;

/**
 * 用户验证
 * Class UserValidate
 * @package app\adminapi\validate\user
 */
class UserValidate extends BaseValidate
{

    /**
     * 设置校验规则
     * @var string[]
     */
    protected $rule = [
        'id' => 'require',
        'user_type' => 'require',
        'real_name' => 'require',
        'nickname' => 'require',
        'account' => 'require|length:1,32|unique:'.User::class,
        'mobile' => 'require',
        'sex' => 'require',
        'is_disable' => 'require',
    ];




    protected $message = [
        'id.require' => '请选择用户',
        'account.require' => '账号不能为空',
        'field.require' => '请选择操作',
        'value.require' => '请输入内容',
        'account.unique' => '账号已存在',
    ];





    /**
     * @notes 用户信息校验
     * @param $value
     * @param $rule
     * @param $data
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkUser($value, $rule, $data)
    {
        $userIds = is_array($value) ? $value : [$value];

        foreach ($userIds as $item) {
            if (!User::find($item)) {
                return '用户不存在！';
            }
        }
        return true;
    }


    /**
     * @notes 校验是否可更新信息
     * @param $value
     * @param $rule
     * @param $data
     * @return bool|string
     */
    public function checkField($value, $rule, $data)
    {
        $allowField = ['account'];

        if (!in_array($value, $allowField)) {
            return '账号不允许更新';
        }

        switch ($value) {
            case 'account':
                //验证手机号码是否存在
                $account = User::where([
                    ['id', '<>', $data['id']],
                    ['account', '=', $data['value']]
                ])->findOrEmpty();

                if (!$account->isEmpty()) {
                    return '账号已被使用';
                }
                break;

            case 'mobile':
                if (false == $this->validate($data['value'], 'mobile', $data)) {
                    return '手机号码格式错误';
                }

                //验证手机号码是否存在
                $mobile = User::where([
                    ['id', '<>', $data['id']],
                    ['mobile', '=', $data['value']]
                ])->findOrEmpty();

                if (!$mobile->isEmpty()) {
                    return '手机号码已存在';
                }
                break;
        }
        return true;
    }


    /**
     * 参数描述
     * @var string[]
     */
    protected $field = [
        'id' => 'id',
        'user_type' => '用户类型',
        'real_name' => '真实姓名',
        'nickname' => '用户昵称',
        'account' => '账号',
        'mobile' => '用户电话',
        'sex' => '用户性别',
        'is_disable' => '是否禁用',
    ];


    /**
     * @notes 添加场景
     * @return UserValidate
     * @author admin
     * @date 2025/04/23 11:42
     */
    public function sceneAdd()
    {
        return $this->only(['user_type','real_name','nickname','account','password','mobile','sex','is_disable']);
    }


    /**
     * @notes 编辑场景
     * @return UserValidate
     * @author admin
     * @date 2025/04/23 11:42
     */
    public function sceneEdit()
    {
        return $this->only(['id','user_type','real_name','nickname','account','password','mobile','sex','is_disable']);
    }


    /**
     * @notes 删除场景
     * @return UserValidate
     * @author admin
     * @date 2025/04/23 11:42
     */
    public function sceneDelete()
    {
        return $this->only(['id']);
    }


    /**
     * @notes 详情场景
     * @return UserValidate
     * @author admin
     * @date 2025/04/23 11:42
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }




}